package com.youlu.campus.web.community.controller;

import com.youlu.campus.entity.VO.req.PointPlaceOrderVO;
import com.youlu.campus.entity.VO.req.QaCommitCommand;
import com.youlu.campus.service.activity.QQMiniActivityService;
import com.youlu.campus.service.contest.QQMiniContestService;
import com.youlu.campus.service.qa.UserQaInfoService;
import com.youlu.campus.web.common.base.RequestContext;
import com.yuelin.infrastructure.quantum.common.QResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

/**
 * QQ小程序竞赛控制器
 */
@RestController
public class QQMiniContestController {


    @Autowired
    private QQMiniContestService qqMiniContestService;

    @Autowired
    private UserQaInfoService userQaInfoService;

    @Autowired
    private QQMiniActivityService qqMiniActivityService;

    /**
     * 用户所有证书
     *
     * @param activityId
     * @return
     */
    @GetMapping("/mini-app/user/certificate-list")
    public QResult userAllCertificate(@RequestParam("activityId") String activityId) {
        return QResult.success(qqMiniContestService.receiveCaList(RequestContext.getCurUser(),activityId));
    }

    /**
     * 授权用户进入活动主页
     *
     * @param activityId
     * @return
     */
    @GetMapping("/mini-app/activity-contest/detail")
    public QResult homePageByActivityId(@RequestParam("activityId") String activityId) {
        return QResult.success(qqMiniContestService.activityDetail(RequestContext.getAppId(),
                RequestContext.getCurUser(),activityId));
    }

    /**
     * 获取邀请进度
     *
     * @param activityId
     * @return
     */
    @GetMapping("/mini-app/activity-contest/invite-progress")
    public QResult activityInviteProgress(@RequestParam("activityId") String activityId) {
        return QResult.success(qqMiniActivityService.getInviteProgress(RequestContext.getCurUser(),2, activityId));
    }


    /**
     * 匿名用户进入活动主页
     *
     * @param activityId 活动ID
     * @return
     */
    @GetMapping("/public/activity-contest/detail")
    public QResult activityDetail(@RequestParam("activityId") String activityId,
                                  @RequestParam("appId") String appId) {
        return QResult.success(qqMiniContestService.activityDetail(appId,RequestContext.getCurUser(),activityId));
    }

    @GetMapping("/public/activity-type")
    public QResult getActivityType(@RequestParam("activityId") String activityId) {
        return QResult.success(qqMiniContestService.getActivityType(activityId));
    }

    /**
     * 答题提交-竞赛
     *
     * @param
     * @return
     */
    @PostMapping("/mini-app/contest/user-qa/commit")
    public QResult qaCommit(@RequestBody @Validated QaCommitCommand commitCommand, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return QResult.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        commitCommand.setCurrentUser(RequestContext.getCurUser());
        //竞赛答题类型
        commitCommand.setType(5);
        return QResult.success(userQaInfoService.doQaCommit(commitCommand));
    }

    /**
     * 纸质证书下单
     *
     * @param req
     * @return
     */
    @PostMapping("/mini-app/contest-ca/place-order")
    public QResult getCaInfo(@NotNull @RequestBody PointPlaceOrderVO req) {
        req.setPlatform(RequestContext.getCurPlatform());
        req.setAppId(RequestContext.getAppId());
        req.setUserId(RequestContext.getUserId());
        req.setActivityType(2);
        return QResult.success(qqMiniActivityService.placeOrderByPoint(req, RequestContext.getCurUser()));
    }

}
